#!/usr/bin/python3.9
# -*- coding: utf-8 -*-
# @Time    : 2021/4/20 12:18 下午
# @Author  : 姜晖
# @Email   : jianghui@tianyancha.com
# @File    : browser.py
from selenium.webdriver import DesiredCapabilities

from base_page import BasePage
from selenium import webdriver


# from pc.page.main_page import MainPage
# from seller.page.login_page import LoginPage as seller_login
# from om.page.login_page import LoginPage as om_login
from ipsa.page.login_page import LoginPage as MainPage
from crm.page.crm_loginpage import CrmlogPage as CrmPage
from utils import load_env
from selenium.common.exceptions import WebDriverException



class Browser(BasePage):
    """
    web浏览器操作，包含启动，退出等
    """

    def launch(self, test_end='ipsa', driver='local', browser='chrome'):
        driver = driver.lower()
        browser = browser.lower()
        if driver == 'local':
            if browser == 'chrome':
                self.driver = webdriver.Chrome()
            elif browser == 'firefox':
                self.driver = webdriver.Firefox()
        elif driver == 'remote':
            if browser == 'chrome':
                self.driver = webdriver.Remote(desired_capabilities=DesiredCapabilities.CHROME)
            elif browser == 'firefox':
                self.driver = webdriver.Remote(desired_capabilities=DesiredCapabilities.FIREFOX)
        env = load_env()
        url = env[test_end.lower()][env['default']]['url']
        self.driver.get(url)
        self.driver.maximize_window()
        self.driver.implicitly_wait(20)
        #self.driver.maximize_window()
        if test_end.lower() == 'ipsa':
            return MainPage(self.driver)
        if test_end.lower()=='crm':
            return CrmPage(self.driver)


    def quit(self):
        try:
            self.driver.quit()
        except WebDriverException:
            pass


